namespace SimpleWebServices.Web.UI.Html
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;
    using Xunit;

    public sealed class InputFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Input>()
                .DerivesFrom<Control>()
                .IsAbstractBaseClass()
                .Implements<IAttributeAccesskey>()
                .Implements<IAttributeAlternativeText>()
                .Implements<IAttributeReadOnly>()
                .Implements<IXmlSerializable>()
                .XmlRoot("input")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Input expected = new InputDerived(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                AlternativeText = "alternative",
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Size = 50,
                TabIndex = 1,
                Value = "value"
            };

            string xml = "<input id='id' class='class' lang='fr' name='id' accesskey='a' alt='alternative' tabindex='1' disabled='disabled' readonly='readonly' size='50' type='derived' value='value' />";
            Input actual = xml.Deserialize<InputDerived>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void serialize()
        {
            Input obj = new InputDerived(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                AlternativeText = "alternative",
                Disabled = true,
                Language = "fr",
                ReadOnly = true,
                Size = 50,
                TabIndex = 1,
                Value = "value"
            };

            string xpath = "1=count(/input[@id='id'][@class='class'][@lang='fr'][@name='id'][@tabindex='1'][@disabled='disabled'][@accesskey='a'][@alt='alternative'][@readonly='readonly'][@size='50'][@type='derived'][@value='value'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new InputDerived() as Input);
        }

        [Fact]
        public void ctor_Name()
        {
            Assert.NotNull(new InputDerived(new Name("id")) as Input);
        }

        [Fact]
        public void ctor_NameNull()
        {
            Assert.NotNull(new InputDerived(null) as Input);
        }

        [Fact]
        public void prop_Accesskey()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("Accesskey"))
                .IsAutoProperty<char?>()
                .Result);
        }

        [Fact]
        public void prop_AlternativeText()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("AlternativeText"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_ReadOnly()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("ReadOnly"))
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Size()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("Size"))
                .IsAutoProperty<int?>()
                .Result);
        }

        [Fact]
        public void prop_Type()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("Type"))
                .DefaultValueIs("derived")
                .Result);
        }

        [Fact]
        public void prop_Value()
        {
            Assert.True(new PropertyExpectations(typeof(InputDerived).GetProperty("Value"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReader_invalid()
        {
            Assert.Throws<InvalidOperationException>(() => "<input type='invalid' />".Deserialize<InputDerived>());
        }
    }
}